package work;

import java.io.*;

public class Test {
    public void addStr(String file, String add) {

        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, true);
            fileWriter.write(add);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

//不完美有问题
//        RandomAccessFile randomFile = null;
//        try {
//            randomFile = new RandomAccessFile(file, "rw");
//            long fileLength = randomFile.length();
//            randomFile.seek(fileLength);
//            randomFile.writeChars(add);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (randomFile != null) {
//                try {
//                    randomFile.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }

    }

    public static void main(String[] args) {
        Test test = new Test();
        test.addStr("d:/a.txt", "你好我好大家好");
    }
}
